/*-------------------------------------------------------------------------------
* file:     app_touchpad.c
* brief:    application
* copyright Copyright (C) 2020-2022 @ NanoIC
* Date:     2023-09-06
* Version:  V1.00
* Author:   
*-------------------------------------------------------------------------------*/

/* include ----------------------------------------------------------------*/
#include "app_touchpad.h"
#include "btl_poweron.h"
#include "app_iic.h"
#include "app_parameter.h"
// #include "app_gsensor.h"
/* externs function--------------------------------------------------------*/

/* externs variablesr------------------------------------------------------*/
extern uint8_t one_wire_bus_app_sleep_or_wakeup_flag;

/* Private variablesr------------------------------------------------------*/
TouchPadDesc_Typedef touch_desc;
uint8_t aTxBuffer[IIC_TX_BUF_LENGTH];
uint8_t aRxBuffer[100];
uint32_t TP_iic_Addr;
uint8_t TP_version=0;
uint32_t TP_ID=0;
volatile uint8_t one_wire_touch_buf[32];

volatile bool tp_init_flag = false;   
volatile bool tp_sleep_up_enable_flag;	//唤醒之后在按键初始化之后读取触摸板数据
uint8_t SLAVE_TEMP_ADDR = 0;

static uint8_t last_send_buffer[30]={0};
//uint8_t tp_last_send_buffer[6];
uint8_t touch_idle_flag=0;
volatile uint8_t touch_tick = 0;
uint8_t touch_tick_flag = 0;

//禁用tp防误触
volatile bool global_tp_alg_botton;
//禁用防误触计时
volatile uint16_t global_tp_alg_tick;

/* Private function--------------------------------------------------------*/
extern volatile bool global_15s_sleep_exit_flag; 

bool BTLTpESDCheckAlive(void);
bool haerware_reset(void);

/**
 * @brief 对单总线收到IIC类设置进行处理
 * 
 * @param p_rec_packet 收到数据包指针
 * 
 * @return true : 成功
 * @return false : 失败
 */
bool one_wrie_bus_app_rec_iic_class_out(SourcePacketStr *p_rec_packet)
{   
//    memset((uint8_t *)&aRxBuffer[0],0,sizeof(aRxBuffer));
//    for(uint16_t i = 0; i < p_rec_packet->len; i++)
//    {
//        aRxBuffer[i] = p_rec_packet->data[i];
//    }
//    #if APP_TP_DEBUG_LOG
//        printf("set TP write = ");
//        console_log_num_byte((uint8_t *)&aRxBuffer[0],p_rec_packet->len);
//    #endif
//    app_iic_write(TOUCHPAD_I2CADDR_GTM1B00,aRxBuffer,p_rec_packet->len);
    return true;
}

/**
 * @brief 对单总线收到IIC类读取进行处理
 * 
 * @param p_rec_packet 收到数据包指针
 * 
 * @return true : 成功
 * @return false : 失败
 */
bool one_wrie_bus_app_rec_iic_class_in(SourcePacketStr *p_rec_packet)
{
    csi_error_t err = CSI_OK;
    uint8_t write_buf[6] = {0};
    memset((uint8_t *)&aRxBuffer[0],0,sizeof(aRxBuffer));
    memset((uint8_t *)&aTxBuffer[0],0,sizeof(aTxBuffer));
    for(uint16_t i = 0; i < 6; i++)
    {
        write_buf[i] = p_rec_packet->data[i];
    }
    #if APP_TP_DEBUG_LOG
        printf("TP Write = ");
        console_log_num_byte((uint8_t *)&write_buf[0],6);
    #endif
    err = app_iic_read_register_buff(TOUCHPAD_I2CADDR_GTM1B00,write_buf,6,aRxBuffer,78);
    if(err != 0x00)
    {
        #if APP_TP_DEBUG_LOG
            printf("write error\n");
        #endif
    }
    #if APP_TP_DEBUG_LOG
        printf("TP Read = ");
        console_log_num_byte((uint8_t *)&aRxBuffer[0],78);
    #endif
    one_wire_bus_app_iic_class_in_ack(aRxBuffer,78);
    return true;
}

/**
 * @brief 读版本tp
 * 
 * @param bin_version 版本号
 */
void tp_read_version(uint16_t bin_version)
{
//    uint8_t cfg_ver;
//    app_iic_read_register(TP_iic_Addr,CFG_VERSION_ADDR,2,(uint8_t *)&cfg_ver,1);
//    TP_version = (bin_version << 8) | cfg_ver;
    #if APP_TP_DEBUG_LOG
        printf("TP_version = %x\n",TP_version);
    #endif
}

/**
 * @brief tp io唤醒状态
 * 
 */
void app_tp_io_set_init(void)
{
	global_tp_alg_botton = false;
    global_tp_alg_tick = 0x00;

    app_iic_io_init();
    csi_gpio_port_dir(GPIOA0, (0x01ul << PINMASK_TP_IIC_INT), GPIO_DIR_INPUT);       // GPIOA1.11 输入
    csi_gpio_port_pull_mode(GPIOA0, (0x01ul << PINMASK_TP_IIC_INT), GPIO_PULLUP);    // 上拉
    #if POWER_SAVING_ENABLE
        csi_pin_irq_mode(PC00, EXI_GRP0, GPIO_IRQ_FALLING_EDGE);
        csi_pin_irq_enable(PC00, EXI_GRP0, ENABLE);	
        csi_vic_set_wakeup_irq(EXI0_IRQ_NUM);
    #endif
}

/// @brief 触摸板初始化
/// @param  
void app_tp_init(void)
{  
	if(tp_init_flag)
		return;
		
    uint8_t check_tp_once;
	static uint8_t check_tp_twice=0;
    csi_error_t err_tp_info_read;

    csi_gpio_port_dir(GPIOA0, (0x01ul << PINMASK_TP_IIC_INT), GPIO_DIR_INPUT);       // GPIOA1.11 输入
    csi_gpio_port_pull_mode(GPIOA0, (0x01ul << PINMASK_TP_IIC_INT), GPIO_PULLUP);    // 上拉
	
//	csi_pin_set_mux(PB02,PB02_GPD);                     
//	csi_pin_pull_mode(UART_TX, GPIO_PULLUP);
	udelay(300);
	TP_version = 0x00;
	
//	BTLTpPowerOnCheck();
#if 1
    for(check_tp_once=0;check_tp_once<3;check_tp_once++)
    {
		TP_iic_Addr = TOUCHPAD_I2CADDR_GTM1B00;
	
	
		if(CSI_OK == csi_iic_read_nbyte(I2C0,TP_iic_Addr,0x0100,2,(uint8_t *)&aTxBuffer[0],30))
		{
			SLAVE_TEMP_ADDR = TOUCHPAD_I2CADDR_GTM1B00;
			if(aTxBuffer[23]==0x78 && aTxBuffer[22]==0x53)
			{
				TP_version=aTxBuffer[24];
				memcpy(&TP_ID,&aTxBuffer[21],4);
			}
			udelay(300);
			break;
		}
    }
	
	
	if(check_tp_twice>3)
	{
		tp_init_flag = true;
	}
	check_tp_twice++;
	
	if(SLAVE_TEMP_ADDR == TOUCHPAD_I2CADDR_GTM1B00)
    {


		//--------------------------
		// 2. Switch To PTP Mode：05 00 33 03 06 00 05 00 03 03 00;
		aTxBuffer[0] = 0x22;
		aTxBuffer[1] = 0x00;
		aTxBuffer[2] = 0x33;
		aTxBuffer[3] = 0x03;
		aTxBuffer[4] = 0x23;
		aTxBuffer[5] = 0x00;
		aTxBuffer[6] = 0x04;
		aTxBuffer[7] = 0x00;
		aTxBuffer[8] = 0x03;
		aTxBuffer[9] = 0x03;
		csi_iic_write_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0500,0,(uint8_t *)&aTxBuffer[0],10);
		udelay(300);

//		//--------------------------
//		// 3. Switch Off Lantency：05 00 37 03 06 00 05 00 07 00 00;
//		aTxBuffer[0] = 0x37;
//		aTxBuffer[1] = 0x03;
//		aTxBuffer[2] = 0x06;
//		aTxBuffer[3] = 0x00;
//		aTxBuffer[4] = 0x05;
//		aTxBuffer[5] = 0x00;
//		aTxBuffer[6] = 0x07;
//		aTxBuffer[7] = 0x00;
//		aTxBuffer[8] = 0x00;
//		csi_iic_write_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0500,2,(uint8_t *)&aTxBuffer[0],9);
//		udelay(300);
//
//		//--------------------------
//		// 4. Enable Finger and Key Report：05 00 35 03 06 00 05 00 05 03 00;
//		aTxBuffer[0] = 0x35;
//		aTxBuffer[1] = 0x03;
//		aTxBuffer[2] = 0x06;
//		aTxBuffer[3] = 0x00;
//		aTxBuffer[4] = 0x05;
//		aTxBuffer[5] = 0x00;
//		aTxBuffer[6] = 0x05;
//		aTxBuffer[7] = 0x03;
//		aTxBuffer[8] = 0x00;
//		csi_iic_write_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0500,2,(uint8_t *)&aTxBuffer[0],9);
//		udelay(300);

	
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_TP_STA,0x01,0x01,0x01);
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_TP_FWVERSION,0x01,0x01,TP_version);
	}else{
		
		rec_single_evt_test(MaintenanceServiceID,REMOTE_EVT_TP_STA,0x01,0x01,0x00);
	}
#endif
	global_tp_alg_botton = false;
    global_tp_alg_tick =1000;

			tp_init_flag = true;
		tp_sleep_up_enable_flag = true;
}


volatile uint32_t tp_irq_pin;
/*
 *  根据IRQ状态读取触模板数据
 */
void app_touch_pad_process(void)
{
   
    static uint8_t lock_flag=0;
    csp_gpio_t *ptGpioBase;
	
	
    if((!tp_init_flag) /*|| (!tp_sleep_up_enable_flag)||ota_first_flag*/)
        return;
		
    ptGpioBase = GPIOA0;
    tp_irq_pin = ptGpioBase->PSDR;
	
    if((tp_irq_pin & (0x01ul << PINMASK_TP_IIC_INT)) == 0 )
	{

		if(one_wire_bus_app_sleep_or_wakeup_flag == 0x01)
		{
			one_wire_bus_app_sleep_or_wakeup_flag = 0x00;
			if(global_15s_sleep_exit_flag)
			{
				global_15s_sleep_exit_flag = false;
				return;
			}
			
		}
		global_tp_alg_botton=false;
		global_tp_alg_tick=1000;
		flash_log_flag=0;
		flash_log_tiner_tick=0;
		app_sleep_timer_clear();
		uint16_t rd_len;
		rd_len = touch_desc.input_max_len;
		if(rd_len > 64)
		{
			rd_len = 64;
		}
		uint8_t fingers = 0;
		if(SLAVE_TEMP_ADDR == TOUCHPAD_I2CADDR_GTM1B00)
		{
			
			//获取触摸板数据
			csi_iic_read_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0000,0,(uint8_t *)&aRxBuffer[0],32);
			
			#if APP_TP_DEBUG_LOG
//					console_log_num_byte((uint8_t *)&one_wire_touch_buf[0],32);
			#endif

		
			if((aRxBuffer[0] == 0x20) && (aRxBuffer[1] == 0x00) && (aRxBuffer[2] == 0x04))
			{
				#if APP_TP_DEBUG_LOG
					// console_log_num_byte((uint8_t *)&aRxBuffer[0],32);
				#endif
				uint8_t head;
				for(uint8_t i=0;i<32;i++)
				{
					one_wire_touch_buf[i] = 0x00;
				}
				one_wire_touch_buf[0] = aRxBuffer[31]; // key
				fingers = aRxBuffer[30];
				//处理前三个字节数据
									
				for(uint8_t i=0;i<29;i++)
				{
				  aRxBuffer[i] = aRxBuffer[i+3];  // 閫傞厤椹卞姩瑕佹眰鐨勬暟鎹牸寮�
				}
				
				for(uint8_t i=0;i<4;i++)
				{
				  aRxBuffer[i+15] = aRxBuffer[i+25];  // 閫傞厤椹卞姩瑕佹眰鐨勬暟鎹牸寮�
				}
				
				if(!(aRxBuffer[0]) && !(aRxBuffer[5]) && !(aRxBuffer[10]))
				{
					aRxBuffer[18]=0;
					return;
				}
				if( kb_lock_state && !lock_flag)
				{
					aRxBuffer[0]&=0x01;
					aRxBuffer[5]&=0x01;
					aRxBuffer[10]&=0x01;
					aRxBuffer[18]=0;
				}else if( kb_lock_state && lock_flag){
					return;
				}else{
					lock_flag=0;
				}
				if((pad_lcd_state==0||pad_cover_state==1)&&(aRxBuffer[18]==0))
					return;
					
//				if(aRxBuffer[0]&0x02||aRxBuffer[5]&0x02||aRxBuffer[10]&0x02)
//				{
//					extern uint16_t global_gsensor_timer_t_tick;
//					global_gsensor_timer_t_tick=0;
//					accel_info_send_flag=0;
//				}else{
//					accel_info_send_flag=1;
//				}
				if(global_key_power_on_flag < 1 )
					return;
				if(delay_key_done_disable_tp_tick==0 && global_weakup_flag)
				{
					one_wire_bus_app_send_touch((uint8_t *)&aRxBuffer[0],fingers);
					memcpy(last_send_buffer,aRxBuffer,19);
				}
				
				#if APP_TP_DEBUG_LOG
//						console_log_num_byte((uint8_t *)&one_wire_touch_buf[0],32);
				#endif
				
			}else{
				
			}

			return;
		}
	}else{
//		if(global_tp_alg_botton)
//		{
//			global_tp_alg_botton=false;
//			global_tp_alg_tick=1000;
//			if(!BTLTpESDCheckAlive())
//			{
//				tp_init_flag=0;	
//				haerware_reset();
//				extern uint16_t global_tp_init_timer_tick;
//				extern uint8_t global_tp_init_timer_flag;
//				global_tp_init_timer_tick=0;
//				global_tp_init_timer_flag=0;
//			}
//			
//		}

	}

}

bool app_touch_pad_big_hand_falg(uint8_t * in_touch_buf, uint8_t f_finger)
{
    uint16_t edge1_x_hand = 0, edge1_y_hand = 0,edge2_x_hand = 0, edge2_y_hand = 0,edge3_x_hand = 0, edge3_y_hand = 0;

	uint16_t flag1_x_hand = 0, flag1_y_hand = 0,flag2_x_hand = 0, flag2_y_hand = 0;

    uint16_t range_big_hand = 500;
    int16_t flag_X = 0, flag_Y = 0;

	uint8_t big_num = 0;
	static uint8_t cnt=0;

	
	if(f_finger>1)
	{
		for(uint8_t i = 0; i < f_finger; i++)
		{
			if(in_touch_buf[i*5]&0x01)
			{
				if(i==0){
					edge1_x_hand = (in_touch_buf[2] << 8) + in_touch_buf[1];
					edge1_y_hand =(in_touch_buf[4] << 8) + in_touch_buf[3];
					
				}else
				if(i==1){
					edge2_x_hand = (in_touch_buf[7] << 8) + in_touch_buf[6];
					edge2_y_hand = (in_touch_buf[9] << 8) + in_touch_buf[8];
				}else
				if(i==2){
					edge3_x_hand = (in_touch_buf[12] << 8) + in_touch_buf[11];
					edge3_y_hand = (in_touch_buf[14] << 8) + in_touch_buf[13];
				}
			}else{
				big_num++;
				if(big_num==1)
				{
					flag1_x_hand = (in_touch_buf[2+i*5] << 8) + in_touch_buf[1+i*5];
					flag1_y_hand = (in_touch_buf[4+i*5] << 8) + in_touch_buf[3+i*5];
				}
				if(big_num==2)
				{
					flag2_x_hand = (in_touch_buf[2+i*5] << 8) + in_touch_buf[1+i*5];
					flag2_y_hand = (in_touch_buf[4+i*5] << 8) + in_touch_buf[3+i*5];
				}
			}
		}
		
	}
    if(big_num)
    {
		for(uint8_t i = 0; i < f_finger; i++)
		{
			if(in_touch_buf[i*5]&0x01)
			{
				edge1_x_hand = (in_touch_buf[2+i*5] << 8) + in_touch_buf[1+i*5];
				edge1_y_hand =(in_touch_buf[4+i*5] << 8) + in_touch_buf[3+i*5];
				flag_X= abs((short)(edge1_x_hand-flag1_x_hand));
				flag_Y= abs((short)(edge1_y_hand-flag1_y_hand));

//				printf("X: %d Y: %d\n",flag_X,flag_Y);
				if(flag_X < range_big_hand && flag_Y < range_big_hand )
				{
					in_touch_buf[i*5]&=0xf2;
				}
				
				if(big_num>1)
				{
					edge1_x_hand = (in_touch_buf[2+i*5] << 8) + in_touch_buf[1+i*5];
					edge1_y_hand =(in_touch_buf[4+i*5] << 8) + in_touch_buf[3+i*5];
					flag_X= abs((short)(edge1_x_hand-flag2_x_hand));
					flag_Y= abs((short)(edge1_y_hand-flag2_y_hand));

					if((flag_X!=0 || flag_Y!=0) && (flag_X < range_big_hand) && (flag_Y < range_big_hand) )
					{
						in_touch_buf[i*5]&=0xf2;
					}
				}
			}

		}
	}
    return true;
}

//2560*1600
volatile uint16_t stag_edge_x[3] = {0}, stag_edge_y[3] = {0};
volatile uint16_t touch_mid_flag = 0x00;
volatile uint8_t record = 1;

volatile uint16_t first_x_f1 = 0, first_y_f1 = 0, first_x_f2 = 0, first_y_f2 = 0;
uint8_t first_touch_flag_f1 = 1, first_touch_flag_f2 = 1;
uint8_t left_flag = 0, right_flag = 0;
uint8_t palm_flag = 0;
bool app_touch_pad_prevent_error_alg(uint8_t * in_touch_buf, uint8_t finger_r,uint8_t hand_number)
{
   uint16_t edge_x = 0, edge_y = 0, edge_x_diff = 0, edge_y_diff = 0;
    uint16_t cm1_left_x = 300, cm1_right_x = 2294, cm1_up_y = 266, cm1_down_y = 1300;
	static uint8_t stag_edge[3]={0};
//    edge_x=(in_touch_buf[2] << 8) + in_touch_buf[1];
//    edge_y=(in_touch_buf[4] << 8) + in_touch_buf[3];

	if(((in_touch_buf[0]&0x02) == 0) && ((in_touch_buf[5]&0x02) == 0)&&((in_touch_buf[10]&0x02) == 0))
	{
		memset(stag_edge_x,0,6);
		memset(stag_edge_y,0,6);
		memset(stag_edge,0,3);
		first_touch_flag_f1 = 1;
		first_touch_flag_f2 = 1;
		// printf("TP UP\n");
		touch_mid_flag = 0;
	}
	if(in_touch_buf[28] == 0x01 && !hand_number)
	{
		touch_idle_flag=0;
		// printf("TP BUTTON\n");
		return true;
	}
	uint16_t temp_x_f1 = 0, temp_y_f1 = 0, temp_x_f2 = 0, temp_y_f2 = 0;
	uint16_t X_f1 = 0, Y_f1 = 0, X_f2 = 0, Y_f2 = 0;
	uint16_t Rx = 256, Ry = 180;


	if(hand_number)
	{
		Rx = 384;
		Ry = 240;
	} else 
	if(finger_r>=2)
	{
		Rx = 120;
		Ry = 90;
	}		
			
	if(finger_r == 3 )
	{	
		if((in_touch_buf[0]&0x01) && (in_touch_buf[5]&0x01) && (in_touch_buf[10]&0x01))
		{
			delay_key_done_2_s_disable_tp_tick = 0;
			printf("TP 22\n");
			touch_tick = 9;
			right_flag = 0;
			left_flag = 0;
			return true;
		}
	}


	for (uint8_t i=0;i<finger_r;i++)
	{
		edge_x=(in_touch_buf[2+5*i] << 8) + in_touch_buf[1+5*i];
		edge_y=(in_touch_buf[4+5*i] << 8) + in_touch_buf[3+5*i];
		uint8_t tp_id=(in_touch_buf[5*i]&0xf0)>>4;
		touch_mid_flag = 0;
		if(in_touch_buf[5*i]&0x01){
			if(delay_key_done_2_s_disable_tp_tick==0){
				if((edge_x<200 && (edge_y<200 || edge_y>1400))||(edge_x>2360 && (edge_y<200 || edge_y>1400)))
				{

				}else
				if(edge_x > cm1_left_x && edge_x < cm1_right_x && edge_y > cm1_up_y && edge_y < cm1_down_y)
				{
					printf("TP 11\n");
					//判断手指在中间区域
					touch_mid_flag = 0x01;
					stag_edge[tp_id]=1;
				}
			}
		}else{
			in_touch_buf[5*i]&=0xf0;
			continue;
		}
								//每次抬起手指（触摸结束）则重置以下flag
		if((in_touch_buf[5*i]&0x02) == 0)
		{
//			touch_mid_flag = 0;
			#if APP_TP_DEBUG_LOG
				printf("TP UP\n");
			#endif
			if(i==1)
			first_touch_flag_f1 = 1;
			if(i==2)
			first_touch_flag_f2 = 1;
			if(stag_edge[tp_id]==0)
			{
				in_touch_buf[5*i]&=0xf0;
			}
			stag_edge_x[tp_id]=0;
			stag_edge_y[tp_id]=0;
			stag_edge[tp_id]=0; 
			// printf("TP UP\n");
			continue;
		}

		if( !touch_mid_flag ) //若不在中间区域
		{
			if(stag_edge[tp_id]==0)
			{
				if(edge_y <= cm1_up_y)
				{ 
					//过滤下边缘
					#if APP_TP_DEBUG_LOG
						// printf("with on\r\n");
					#endif
					in_touch_buf[5*i]&=0xf0;
					continue;
				}
				if(((edge_x <= cm1_left_x || edge_x >= cm1_right_x || edge_y >= cm1_down_y) && edge_y>=cm1_up_y )\
					||(edge_x<200 && (edge_y<200 || edge_y>1400))||(edge_x>2360 && (edge_y<200 || edge_y>1400))\
						||(delay_key_done_2_s_disable_tp_tick>0))
				{ 
					//上左右边缘滑动超过1cm有效
	//				if(record)
					if(stag_edge_x[tp_id] == 0 && stag_edge_y[tp_id] == 0)
					{
						if(tp_id<3){
						//记录首个按下的边缘手指坐标
							stag_edge_x[tp_id] = edge_x;
							stag_edge_y[tp_id] = edge_y;
						}else{
							return false;
						}
						record = 0x00;
					}

					if(edge_x >= stag_edge_x[tp_id])
					{
						//edge_x_diff 是手指在边缘移动的距离
						edge_x_diff = edge_x - stag_edge_x[tp_id];
					}else{
						edge_x_diff = stag_edge_x[tp_id] - edge_x;
					}
					if(edge_y >= stag_edge_y[tp_id])
					{
						edge_y_diff = edge_y - stag_edge_y[tp_id];
					}else{
						edge_y_diff = stag_edge_y[tp_id] - edge_y;
					}
					if(edge_x_diff <= Rx && edge_y_diff <= Ry)
					{
						//判断移动小于100不上报坐标
						#if APP_TP_DEBUG_LOG
							// printf("no 100\r\n");
						#endif
	//					return false;
						in_touch_buf[5*i]&=0xf0;
						//  printf("no 100\r\n");
					}else{
						stag_edge[tp_id]=1;
							// printf("TP 44\n");
	//						return true;
					}	
						
					
				}

			}
		}

	}
		
	
    return true;
}


bool app_touch_pad_big_hand_alg(uint8_t * in_touch_buf, uint8_t f_finger, uint8_t big_hand_num)
{
    // uint16_t edge1_x_hand = 0, edge1_y_hand = 0,edge2_x_hand = 0, edge2_y_hand = 0,edge3_x_hand = 0, edge3_y_hand = 0;

    // uint16_t range_big_hand = 1000;
    // int16_t right_X = 0, lift_X = 0, up_Y = 0, done_Y = 0;

    // uint8_t in_touch_buf[32] = {0};

    // if(big_num == 1)
    // {
    //     edge1_x_hand=(in_hand_buf[1] << 8) + in_hand_buf[0];
    //     edge1_y_hand=(in_hand_buf[3] << 8) + in_hand_buf[2];
    //     //一个大手掌

    //         edge2_x_hand=(in_touch_buf[3] << 8) + in_touch_buf[2];
    //         edge2_y_hand=(in_touch_buf[5] << 8) + in_touch_buf[4];
    //         edge3_x_hand=(in_touch_buf[8] << 8) + in_touch_buf[7];
    //         edge3_y_hand=(in_touch_buf[10] << 8) + in_touch_buf[9];

        
    //     right_X = edge1_x_hand + range_big_hand;
    //     lift_X = edge1_x_hand - range_big_hand;
    //     up_Y = edge1_y_hand + range_big_hand; //up + right +
    //     done_Y = edge1_y_hand - range_big_hand;

    //     if(lift_X < 0)
    //     {
    //         lift_X = 0;
    //     }
    //     if(done_Y < 0)
    //     {
    //         done_Y = 0;
    //     }
    //     if(edge2_x_hand < right_X && edge2_x_hand > lift_X)
    //     {
    //         if(edge2_y_hand < up_Y && edge2_y_hand > done_Y)
    //         {
    //             return false;
    //         }
    //     }
    //     if(edge3_x_hand < right_X && edge3_x_hand > lift_X)
    //     {
    //         if(edge3_y_hand < up_Y && edge3_y_hand > done_Y)
    //         {
    //             return false;
    //         }
    //     }
    // }
    return true;
}




void app_tp_lowpower_set_tp(bool on)
{
    uint8_t send_buf[2] = {0};
    send_buf[1] = 0x08;
    if(on)
    {
        send_buf[0] = 0x00;
    }else
    {
        send_buf[0] = 0x01;
    }
    csi_iic_write_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x2200,2,(uint8_t *)&send_buf[0],2);
}

/**
 * @brief tp休眠函数
 * 
 * @param on true唤醒 false 休眠
 */
//void app_touch_pad_lowpower_io_set(bool on)
//{
//    if(on)
//    {
//		global_tp_alg_tick = 200;
  //      csi_pin_irq_enable(PA110, EXI_GRP0, DISABLE);
    //    app_tp_io_set_init();
//		app_tp_lowpower_set_tp(true);
//		udelay(200);
 //   }else
   // {
		
//		app_tp_lowpower_set_tp(false);
//		udelay(200);
//		//休眠前禁用掉防误触
//        global_tp_alg_botton = true;
 //       app_iic_io_sleep_set();
//        csi_pin_set_mux(PA110,PA110_INPUT);
//        csi_pin_pull_mode(PA110, GPIO_PULLUP);
 //       csi_pin_irq_mode(PA110, EXI_GRP10, GPIO_IRQ_FALLING_EDGE);
 //       csi_pin_irq_enable(PA110, EXI_GRP10, ENABLE);	
 //       csi_vic_set_wakeup_irq(EXI4_IRQ_NUM);
//		tp_sleep_up_enable_flag = false;
 //   }
//}

/**
 * @brief tp待机函数
 * 
 * @param on true唤醒 false 休眠
 */
void app_touch_pad_doze_io_set(bool on)
{
    if(on)
    {
		global_tp_alg_tick = 200;
        csi_pin_irq_enable(PA00, EXI_GRP0, DISABLE);
        app_tp_io_set_init();
    }else
    {
	
		//休眠前禁用掉防误触
        global_tp_alg_botton = true;
        app_iic_io_sleep_set();
        csi_pin_set_mux(PA00,PA00_INPUT);
        csi_pin_pull_mode(PA00, GPIO_PULLUP);
        csi_pin_irq_mode(PA00, EXI_GRP0, GPIO_IRQ_FALLING_EDGE);
        csi_pin_irq_enable(PA00, EXI_GRP0, ENABLE);	
        csi_vic_set_wakeup_irq(EXI0_IRQ_NUM);
		tp_sleep_up_enable_flag = false;
    }
}


/*
增加ESD心跳机制需注意如下：
	1、建议心跳机制间隔在1s左右，该指令优先级较低，可在IIC总线空闲时调用即可；
    2、IIC写入0x01,0x00与读一个字节之间，stop+start信号改成restart；（避免心跳机制指令被冲突）
    3、IIC处于idle的200ms之后才发送心跳机制命令，即IIC busy的200ms内不能发；（避免心跳机制冲突总线）
*/

/* ESD reset */
/* return  false : hardware reset  */
bool BTLTpESDCheckAlive(void)
{
        uint8_t hidDescCmd[2] = {0x01, 0x00};
        uint8_t hidDesc = 0;
//        int res = BTL_I2CSendRecvEx(hidDescCmd, 2, &hidDesc, 1);
		csi_error_t res = csi_iic_read_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0100,2,(uint8_t *)&hidDesc,1);
        //retry
        if (res || hidDesc != 0x1E)
        {
			udelay(200);
			res = csi_iic_read_nbyte(I2C0,TOUCHPAD_I2CADDR_GTM1B00,0x0100,2,(uint8_t *)&hidDesc,1);
        }

        if (res || hidDesc != 0x1E)
        {
             return false;
        }
        else
        {
             return true;
        }
}



bool haerware_reset(void)
{
        csi_pin_set_mux(PB02,PB02_OUTPUT);                     
        csi_pin_pull_mode(UART_TX, GPIO_PULLNONE);
		csi_gpio_port_set_low(GPIOB0, (0x01ul << 0));
		
		
}


